using System;
using System.Data;

using WP.Commons;
using WP.DB;
using WP.MF.DataEntity; 

namespace WP.MF.DataAccess
{
	/// <summary>
	/// Summary description for MUser.
	/// </summary>
    public class MUserDAL : IDataAccess
	{
		public MUserDAL()
		{
			//
			// TODO: Add constructor logic here
			//
		}

        #region Select List
        public string SQLList(int iParameter)
        {
            string strSQL = @"SELECT RecId,UserName,Description,Title,Phone,rmSecurGrp FROM mUser order by UserName ";
            return strSQL;
        }
        #endregion

        #region Fields
        public string Fields
        {
            get
            {
                return @"UserName,Title,glSecurGrp,apSecurGrp,arSecurGrp,oeSecurGrp,imSecurGrp,poSecurGrp,rmSecurGrp,
                        twSecurGrp,tpSecurGrp,smSecurGrp,mfSecurGrp,Language,Description,InitialName,AbleToExcel,
                        Phone,AbleToChangeBOMFormula,AbleToChangeFormula,approval1,approval2,approval3,approval4,approval5,
                        AbleToPostWorkQty,AbleToPostSTDSalary,AbleToOtherSalary
                        ";
            }
        }
        #endregion

        #region Insert MUser by DataRow
        public void InsertMUser(DBAccess dBAccess, MUserDEL mUserDEL)
        {
            string strSql = string.Empty;
            DataRow dr = mUserDEL.Tables[0].Rows[0];

            strSql = BaseFunctions.GenerateSqlForDataAccess(Fields, MUserDEL.TABLENAME_MUSER, dr, FormStatus.AddNew);

            dr[MUserDEL.RECID] = dBAccess.GetObjectValue(strSql);
        }
        #endregion

        #region Update MUser by DataRow
        public void UpdateMUser(DBAccess dBAccess, MUserDEL mUserDEL)
        {
            string strSql = string.Empty;
            DataRow dr = mUserDEL.Tables[0].Rows[0];
            strSql = BaseFunctions.GenerateSqlForDataAccess(Fields, MUserDEL.TABLENAME_MUSER, dr, FormStatus.Update);

            dr[MUserDEL.RECSTAMP] = dBAccess.GetObjectValue(strSql);
        }
        #endregion

        #region Delete MUser by Key
        public void DeleteMUser(DBAccess dBAccess, int iRecID)
        {
            string strSql = string.Empty;

            strSql = @"DELETE FROM mUser WHERE RecID=" + iRecID;

            dBAccess.ExecuteSql(strSql);
        }
        #endregion

        #region Copy User
        public void CopyMUser(DBAccess dBAccess, string strOldName, string strNewName, string strNewDesc)
        {
            string strSql = string.Empty;
            string strSqlTemp = string.Empty;

            strSqlTemp = DataFunctions.GetTableSQL(dBAccess, "mUser");
            strSqlTemp = strSqlTemp.Replace("[Description],", "");
            strSqlTemp = strSqlTemp.Replace("[Password],", "");

            strSql = " insert into mUser (" + strSqlTemp + ")";
            strSql += "\r\n" + " select '" + strNewName + "', " + strSqlTemp.Replace("[UserName],", "");
            strSql += " from muser where UserName='" + strOldName + "'";
            dBAccess.ExecuteSql(strSql);

            strSql = " update mUser set description='" + strNewDesc + "' where UserName='" + strNewName + "'";
            dBAccess.ExecuteSql(strSql);

            strSql = "insert into mUserBranch ([UserName],[BranchNo],[Access],[glAccess])";
            strSql += "\r\n" + "select '" + strNewName + "',[BranchNo],[Access],[glAccess] from mUserBranch where UserName='" + strOldName + "'";
            dBAccess.ExecuteSql(strSql);

            strSql = "insert into mUserTrader ([Username],[SlspNo],[SlspName],[Access],[PoAccess])";
            strSql += "\r\n" + "select '" + strNewName + "',[SlspNo],[SlspName],[Access],[PoAccess] from mUserTrader where UserName='" + strOldName + "'";
            dBAccess.ExecuteSql(strSql);

            strSql = "insert into mUserLocation ([UserName],[Location],[LocationDesc],[Access])";
            strSql += "\r\n" + "select '" + strNewName + "',[Location],[LocationDesc],[Access] from mUserLocation where UserName='" + strOldName + "'";
            dBAccess.ExecuteSql(strSql);

            strSql = "insert into mUserBid ([UserName],[SlspNo],[SlspName],[BidAccess],[JobAccess])";
            strSql += "\r\n" + "select '" + strNewName + "',[SlspNo],[SlspName],[BidAccess],[JobAccess] from mUserBid where UserName='" + strOldName + "'";
            dBAccess.ExecuteSql(strSql);

        }
        #endregion

        #region ***Old Code

        public string SelectUserForLookUp()
        {
            return "SELECT UserName,Description,Language, BranchNo, OrdBranchNo, Location,AccessAllSalesOrder FROM mUser";
        }

        public string SelectByUserName(string strUserName)
        {
            string strSql = "";
            strSql = "SELECT * FROM mUser WHERE RTRIM(UserName)='" + strUserName + "'";
            return strSql;
        }
        public string SelectOneByUserName(string strUsername)
        {
            string strSql = "";
            strSql = "SELECT Location FROM mUser WHERE RTRIM(UserName)='" + strUsername + "'";
            return strSql;
        }
        public string SelectOneByUserName(string strUserName, string fieldName)
        {
            string strSql = "";
            strSql = "SELECT " + fieldName + " FROM mUser WHERE RTRIM(UserName)='" + strUserName + "'";
            return strSql;

        }

        public string SelectForMenuTree()
        {
            string strSql = "";
            string strGroup = "";

            strGroup = GetGroupFieldNameByModule();
            strSql = "SELECT {0} FROM mUser WHERE RTRIM(UserName)='{1}'";
            strSql = string.Format(strSql, strGroup, SysVar.UserName);

            return strSql;
        }

        public string GetGroupFieldNameByModule()
        {
            string strGroup = "";

            switch (SysVar.CurrentSystemModule)
            {
                case SystemModules.GL:
                    strGroup = "glSecurGrp";
                    break;
                case SystemModules.AP:
                    strGroup = "apSecurGrp";
                    break;
                case SystemModules.AR:
                    strGroup = "arSecurGrp";
                    break;
                case SystemModules.OE:
                    strGroup = "oeSecurGrp";
                    break;
                case SystemModules.IM:
                    strGroup = "imSecurGrp";
                    break;
                case SystemModules.PO:
                    strGroup = "poSecurGrp";
                    break;
                case SystemModules.RM:
                    strGroup = "rmSecurGrp";
                    break;
                case SystemModules.TW:
                    strGroup = "twSecurGrp";
                    break;
                case SystemModules.TP:
                    strGroup = "tpSecurGrp";
                    break;
                case SystemModules.ES:
                    strGroup = "smSecurGrp";
                    break;
                case SystemModules.MF:
                    strGroup = "mfSecurGrp";
                    break;
            }
            return strGroup;
        }

        #endregion***



        #region User Alert
        public void InsertmUserAlert(DBAccess dBAccess, DataRow dr)
        {
            string strSql = string.Empty;
            string strFields = "UserName,Module,AlertPrompt,AlertCondition,TableName,PromptFont,PromptColor,priority";
            strSql = BaseFunctions.GenerateSqlForDataAccess(strFields, "mUserAlert", dr, FormStatus.AddNew);

            dr[MUserDEL.RECID] = dBAccess.GetObjectValue(strSql);
        }

        public void UpdatemUserAlert(DBAccess dBAccess, DataRow dr)
        {
            string strSql = string.Empty;
            string strFields = "UserName,Module,AlertPrompt,AlertCondition,TableName,PromptFont,PromptColor,priority";
            strSql = BaseFunctions.GenerateSqlForDataAccess(strFields, "mUserAlert", dr, FormStatus.Update);

            dr[MUserDEL.RECSTAMP] = dBAccess.GetObjectValue(strSql);
        }

        public void DeletemUserAlert(DBAccess dBAccess, int iRecID)
        {
            string strSql = string.Empty;

            strSql = @"DELETE FROM mUserAlert WHERE RecID=" + iRecID;

            dBAccess.ExecuteSql(strSql);
        }
        #endregion

    }
}
